/** ***************** Position ****************************** */
function getPositionDetails(baseUrl, positionSelctionBoxId) {
	var positionSelectBox = document.getElementById(positionSelctionBoxId);
	var positionId = positionSelectBox.options[positionSelectBox.selectedIndex].value;
	document.location.href = baseUrl + "/setupPositionForm.ems?positionId=" + positionId;
}

function deletePosition(baseUrl, positionId) {
	document.location.href = baseUrl + "/deletePosition.ems?positionId=" + positionId;
}

/** ***************** ClassAndSection ****************************** */
function getClassDetails(baseUrl, classSelctionBoxId) {
	var classSelectBox = document.getElementById(classSelctionBoxId);
	var classId = classSelectBox.options[classSelectBox.selectedIndex].value;
	document.location.href = baseUrl + "/setupClassSectionManagementForm.ems?classId=" + classId;
}

function deleteClass(baseUrl, classId) {
	document.location.href = baseUrl + "/deleteClassSection.ems?classId=" + classId;
}

/** ***************** School ****************************** */
function getSchoolDetails(baseUrl, schoolSelectionBoxId) {
	var schoolSelectBox = document.getElementById(schoolSelectionBoxId);
	var schoolId = schoolSelectBox.options[schoolSelectBox.selectedIndex].value;
	document.location.href = baseUrl + "/setupSchoolManagementForm.ems?schoolId=" + schoolId;
}

function deleteClass(baseUrl, schoolId) {
	document.location.href = baseUrl + "/deleteSchool.ems?schoolId=" + schoolId;
}

var MARKING_PERIOD_ACTION = 1;
var GRADING_RULE_ACTION = 2;

var markingPeriodTable_Id = "markingPeriodTable";
var markingPeriodRow_Id = "markingPeriodRow_";
var markingPeriodTitle_Id = "markingPeriodTitle_";
var markingPeriodTitle_Name = "markingPeriodList[index].markingPeriodTitle";
var markingPeriodStartDate_Id = "markingPeriodStateDate_";
var markingPeriodStartDate_Name = "markingPeriodList[index].markingPeriodStartDate";
var markingPeriodEndDate_Id = "markingPeriodEndState_";
var markingPeriodEndDate_Name = "markingPeriodList[index].markingPeriodEndDate";

var gradingRuleTable_Id = "gradingRuleTable";
var gradingRuleRow_Id = "gradingRuleRow_";
var gradingRuleFrom_Id = "gradingRuleFrom_";
var gradingRuleFrom_Name = "gradingRules[index].fromMarks";
var gradingRuleTo_Id = "gradingRuleTo_";
var gradingRuleTo_Name = "gradingRules[index].toMarks"
var gradingRuleGrade_Id = "gradingRuleGrade_";
var gradingRuleGrade_Name = "gradingRules[index].grade";

function addMarkingAndGradingRow(action, imageBashUrl) {

	var tableId, rowId, firstCellId, firstCellName, secondCellId, secondCellName, thirdCellId, thirdCellName;

	if (action == MARKING_PERIOD_ACTION) {
		tableId = markingPeriodTable_Id;
		rowId = markingPeriodRow_Id;
		firstCellId = markingPeriodTitle_Id;
		firstCellName = markingPeriodTitle_Name;
		secondCellId = markingPeriodStartDate_Id;
		secondCellName = markingPeriodStartDate_Name;
		thirdCellId = markingPeriodEndDate_Id;
		thirdCellName = markingPeriodEndDate_Name;
	} else if (action == GRADING_RULE_ACTION) {
		tableId = gradingRuleTable_Id;
		rowId = gradingRuleRow_Id;
		firstCellId = gradingRuleFrom_Id;
		firstCellName = gradingRuleFrom_Name;
		secondCellId = gradingRuleTo_Id;
		secondCellName = gradingRuleTo_Name;
		thirdCellId = gradingRuleGrade_Id;
		thirdCellName = gradingRuleGrade_Name;
	}

	var table = document.getElementById(tableId);
	var rowCount = table.rows.length;
	var newIndex = rowCount - 2;
	
	var row = table.insertRow(rowCount - 1);
	row.id = rowId + newIndex;

	var firstCell = row.insertCell(0);
	var firstCellElement = document.createElement("input");
	firstCellElement.id = firstCellId + newIndex;
	firstCellElement.name = firstCellName.replace("index", newIndex);
	firstCell.appendChild(firstCellElement);

	var secondCell = row.insertCell(1);
	var secondCellElement = document.createElement("input");
	secondCellElement.id = secondCellId + newIndex;
	secondCellElement.name = secondCellName.replace("index", newIndex);
	secondCell.appendChild(secondCellElement);

	var thirdCell = row.insertCell(2);
	var thirdCellElement = document.createElement("input");
	thirdCellElement.id = thirdCellId + newIndex;
	thirdCellElement.name = thirdCellName.replace("index", newIndex);
	thirdCell.appendChild(thirdCellElement);

	var deleteImageCell = row.insertCell(3);

	var deleteImgHyperLink = document.createElement("a");
	deleteImgHyperLink.title = "Delete";
	deleteImgHyperLink.style = "border-width: 0px;";
	deleteImgHyperLink.href = "javascript:deleteMarkingGradingRow(" + action + "," + newIndex + ")";

	var deleteImg = document.createElement("img");
	deleteImg.src = imageBashUrl + "/delete.png";
	deleteImgHyperLink.appendChild(deleteImg);

	deleteImageCell.appendChild(deleteImgHyperLink);
}

function deleteMarkingGradingRow(action, deletingRowIndex) {

	var tableId, rowId, firstCellId, firstCellName, secondCellId, secondCellName, thirdCellId, thirdCellName;

	if (action == MARKING_PERIOD_ACTION) {
		tableId = markingPeriodTable_Id;
		rowId = markingPeriodRow_Id;
		firstCellId = markingPeriodTitle_Id;
		firstCellName = markingPeriodTitle_Name;
		secondCellId = markingPeriodStartDate_Id;
		secondCellName = markingPeriodStartDate_Name;
		thirdCellId = markingPeriodEndDate_Id;
		thirdCellName = markingPeriodEndDate_Name;
	} else if (action == GRADING_RULE_ACTION) {
		tableId = gradingRuleTable_Id;
		rowId = gradingRuleRow_Id;
		firstCellId = gradingRuleFrom_Id;
		firstCellName = gradingRuleFrom_Name;
		secondCellId = gradingRuleTo_Id;
		secondCellName = gradingRuleTo_Name;
		thirdCellId = gradingRuleGrade_Id;
		thirdCellName = gradingRuleGrade_Name;
	}

	var table = document.getElementById(tableId);
	alert(deletingRowIndex + 1);
	table.deleteRow(deletingRowIndex + 1);

	var rowCount = table.rows.length;

	for ( var i = deletingRowIndex; i < rowCount; i++) {
		var row = document.getElementById(rowId + (deletingRowIndex + 1));
		var replaceIndex = deletingRowIndex - 1;
		row.id = rowId + replaceIndex;

		var firstCellElement = document.getElementById(firstCellId + (deletingRowIndex + 1));
		firstCellElement.id = firstCellId + replaceIndex;
		firstCellElement.name = firstCellName.replace("index", replaceIndex);

		var secondCellElement = document.getElementById(secondCellId + (deletingRowIndex + 1));
		secondCellElement.id = secondCellId + replaceIndex;
		secondCellElement.name = secondCellName.replace("index", replaceIndex);

		var thirdCellElement = document.getElementById(thirdCellId + (deletingRowIndex + 1));
		thirdCellElement.id = thirdCellId + replaceIndex;
		thirdCellElement.name = thirdCellName.replace("index", replaceIndex);
	}
}